<%--
  Created by IntelliJ IDEA.
  User: Gao
  Date: 2022/12/27
  Time: 8:45
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <%@include file="header.jsp" %>
</head>
<body>
    <form id="formId" class="layui-form layui-form-pane" action="">
        <input type="hidden" name="id" value="${user.id}"/>
        <div class="layui-form-item">
            <label class="layui-form-label">用户名</label>
            <div class="layui-input-block">
                <input type="text" name="username" value="${user.username}" autocomplete="off" placeholder="请输入标题" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">密码</label>
            <div class="layui-input-block">
                <input type="text" name="password" value="${user.password}" autocomplete="off" placeholder="请输入标题" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">年龄</label>
            <div class="layui-input-block">
                <input type="text" name="age" value="${user.age}" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <button class="layui-btn" type="button" onclick="submitForm()">添加</button>
        </div>
    </form>

    <script>
        function submitForm() {
            $.post(
                '/user/update',
                $('#formId').serialize(), //{'name':'张三','password':123,'age':23}
                function(jsonResult) {
                    if (jsonResult.ok) {
                        //mylayer.okMsg('添加成功');
                        //假设这是iframe页
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        layer.msg(
                            '添加成功',
                            {icon:1, time:1000},
                            function() {
                                parent.layer.close(index); //再执行关闭
                                window.parent.location.reload();
                            }
                        );
                    } else {
                        mylayer.errorMsg('添加失败');
                    }
                },
                'json'
            );
        }
    </script>
</body>
</html>
